<?php

use Illuminate\Database\Seeder;

class DeleteRepeatedUserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $openids = App\Models\Participant\Participant::query()
            ->select('openid')
            ->selectRaw('count(openid) as co')
            ->groupBy('openid')
            ->get();

        foreach ($openids as $val) {
            $repeated_num = $val->co;
            if($repeated_num > 1) {
                \Illuminate\Support\Facades\DB::table('participants')
                    ->where('openid', $val->openid)
                    ->limit($repeated_num - 1)
                    ->delete();
            }
        }

    }

}
